<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="指定发放">
        <a-form :model="formState">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="卡券名称">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.name"
                  placeholder="输入活动名称"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="卡券编号">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.couponCode"
                  placeholder="输入卡券编号"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="卡券类别">
                <a-select
                  ref="select"
                  v-model:value="formState.couponCategory"
                  style="width: 100%"
                  placeholder="卡券类别"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">购物券</a-select-option>
                  <a-select-option :value="2">鉴定券</a-select-option>
                  <a-select-option :value="3">养护券</a-select-option>
                  <a-select-option :value="4">找货券</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="使用类目">
                <a-select
                  ref="select"
                  v-model:value="formState.typeId"
                  style="width: 100%"
                  placeholder="使用类目"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option
                    v-for="item in goodsTypeList"
                    :key="item.id"
                    :value="item.id"
                    >{{ item.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="发放群体">
                <a-select
                  ref="select"
                  mode="multiple"
                  v-model:value="formState.groupType"
                  style="width: 100%"
                  placeholder="活动角色"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">新用户</a-select-option>
                  <a-select-option :value="2">老用户</a-select-option>
                  <a-select-option :value="3">会员用户</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="抵扣方式">
                <a-select
                  ref="select"
                  v-model:value="formState.type"
                  style="width: 100%"
                  placeholder="抵扣方式"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">满减</a-select-option>
                  <a-select-option :value="2">折扣</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handleAdd">新建</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="appointColumns"
          :pagination="pagination"
          @change="handleChangeSize"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'couponCategory'">
              {{
                record.couponCategory === 1
                  ? "购物券"
                  : record.couponCategory === 2
                  ? "鉴定券"
                  : record.couponCategory === 3
                  ? "养护券"
                  : record.couponCategory === 4
                  ? "找货券"
                  : ""
              }}
            </template>
            <template v-if="column.key === 'typeId'">
              {{
                record.typeId.join(",") == 0
                  ? "全部"
                  : filterTypeIds(record.typeId)
              }}
            </template>
            <template v-if="column.key === 'groupType'">
              {{ groupTypeNames(record.groupType) }}
            </template>
            <template v-if="column.key === 'validDay'">
              <span v-if="record.validType == 1">{{ record.validDay }}</span>
              <span v-else>{{ record.startTime }}-{{ record.endTime }}</span>
            </template>
            <template v-if="column.key === 'type'">
              {{ record.type == 1 ? "满减" : "折扣" }}
            </template>
            <template v-if="column.key === 'state'">
              {{ record.state == 1 ? "启用" : "禁用" }}
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button type="primary" @click="handleSend(record.id)"
                  >发放</a-button
                >
              </a-space>
            </template>
          </template>
        </a-table>
      </a-card>
    </a-space>
    <sendCoupons ref="sendCouponsRef" @refreshList="refreshList" />
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { appointColumns } from "./columns.js";
import { listCouponConfig } from "@/api/market";
import { goodsType } from "@/api/order";
import { useRouter, useRoute } from "vue-router";
import sendCoupons from "./components/sendCoupons.vue";
const router = useRouter();
const pagination = ref({
  current: 1,
  pageSize: 10,
  defaultPageSize: 10,
  total: 0,
  showSizeChanger: true,
  pageSizeOptions: ["10", "20", "50", "100"],
  showTotal: (total) => `共 ${total} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  state: 1,
  name: undefined,
  couponCode: undefined,
  couponCategory: undefined,
  typeId: undefined,
  groupType: undefined,
  type: undefined,
  source: 1,
});
const groupTypeArr = ref([
  {
    id: 0,
    name: "全部",
  },
  {
    id: 1,
    name: "新用户",
  },
  {
    id: 2,
    name: "老用户",
  },
  {
    id: 3,
    name: "会员用户",
  },
]);
const groupTypeNames = (ids) => {
  return ids.join(",") == 0
    ? "全部"
    : groupTypeArr.value
        .map((v) => {
          if (ids.includes(v.id)) {
            return v.name;
          }
        })
        .filter((item) => item != undefined)
        .join("/");
};
// 获取卡券列表数据
const dataSource = ref([]);
const getListCouponConfigFn = async () => {
  const res = await listCouponConfig(formState.value);
  if (res.code === 200) {
    dataSource.value = res.data.list;
    pagination.value.total = res.data.total;
  }
};
// 获取类目
const goodsTypeList = ref([]);
const getGoodsType = async () => {
  const result = await goodsType();
  if (result.code == 200) {
    goodsTypeList.value = result.data;
  }
};
// 过滤类型
const filterTypeIds = (ids) => {
  return goodsTypeList.value
    .map((item) => {
      if (ids.includes(item.id)) {
        return item.name;
      }
    })
    .filter((item) => item != undefined)
    .join("/");
};
function handleSearch() {
  formState.value.page = 1;
  formState.value.pageSize = 10;
  getListCouponConfigFn();
}
// 新建
function handleAdd() {
  router.push("/marketManage/voucher");
}
function handleReset() {
  formState.value = {
    page: 1,
    pageSize: 10,
    state: 1,
    source: 1,
    name: undefined,
    couponCode: undefined,
    couponCategory: undefined,
    typeId: undefined,
    groupType: undefined,
    type: undefined,
  };
  pagination.value.current = 1;
  pagination.value.pageSize = 10;
  getListCouponConfigFn();
}
const sendCouponsRef = ref(null);
function handleSend(id) {
  sendCouponsRef.value.open(id);
}
function refreshList() {
  getListCouponConfigFn();
}
function handleChangeSize(page) {
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  pagination.value.current = page.current;
  pagination.value.pageSize = page.pageSize;
  getListCouponConfigFn();
}
onMounted(() => {
  getGoodsType();
  getListCouponConfigFn();
});
</script>

<style lang="less">
.img-item {
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}
</style>
